<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（上海）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;

/**
 * 储值管理
 * @package app\erp\model
 */
class UcMemberBalanceJournal extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member_balance_journal';

    /**
     * 查询数据
     * @param id int 优惠券id
     * @author jason
     * @return array_out(code, message, (int)id)
     */
    public function getInfoById($id, $cache = true){
        $info = $this -> cache($cache) -> find($id);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取储值表信息
     */
    public function getInfo($map)
    {
        $info = $this
            -> alias('j')
            -> leftJoin('xm_uc_member u','j.uid = u.uid')
            -> where($map)
            ->field('j.*,u.nick_name')
            -> order('j.create_time desc')
            -> paginate();

        if($info){
            foreach ($info as $key=>$value){
                $info[$key]['log'] = model('UcMemberBalanceJournalLog', 'model')->where('journal_id','=',$value['id'])->order('create_time asc')->column('*');
            }
            $total = [
                'total_price' => 0,
                'total_uid' => 0,
                'total_order' => 0,
                'total_song_price' => 0,
                'total_price_in' => 0,
                'total_price_e' => 0,
            ];
            $item = [];
            foreach ($info as $key => $value) {
                //计算 总价值
                $total['total_price'] += $value['pay_price'];
                if(!isset($item['uid'][$value['uid']])){
                    $item['uid'][$value['uid']] = $value['uid'];
                    $total['total_uid'] += 1;
                }else{}
                if(!isset($item['pay_order_id'][$value['pay_order_id']])){
                    $item['pay_order_id'][$value['pay_order_id']] = $value['pay_order_id'];
                    $total['total_order'] += 1;
                }else{}
            }
            $infos['total'] = $total;
            $infos['info'] = $info;
            return array_out(200, 'success', $infos);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 储值统计-获取全部信息
     * @param $map
     * @return array
     * @throws \think\exception\DbException
     */
    public function getAllInfo($map)
    {
        //
        //交易明细
        $info = $this
            ->alias('j')
            ->leftJoin('xm_uc_member_balance_journal_log l', 'j.id = l.journal_id')
            ->leftJoin('xm_uc_member u', 'j.uid = u.uid')
            ->where($map)
            ->field('j.*,l.id as lid,l.journal_id,l.uid as luid,l.ine,l.price,l.use_rule,l.log_remark,l.create_time as lcreate_time,u.nick_name')
            ->order('j.create_time desc')
            ->paginate();
        if ($info) {
            $total = [
                'total_price' => 0,
                'total_uid' => 0,
                'total_order' => 0,
                'total_song_price' => 0,
                'total_price_in' => 0,
                'total_price_e' => 0,
            ];
            $item = [];
            foreach ($info as $key => $value) {
                //计算 总价值
                if(!isset($item['uid'][$value['uid']])){
                    $item['uid'][$value['uid']] = $value['uid'];
                    $total['total_uid'] += 1;
                }else{}
                if(!isset($item['pay_order_id'][$value['pay_order_id']])){
                    $item['pay_order_id'][$value['pay_order_id']] = $value['pay_order_id'];
                    $total['total_order'] += 1;
                }else{}
                //收入支出统计
                if($value['ine'] == 3){
                    $total['total_price_e'] += $value['price'];
                }elseif($value['ine'] == 2){
                    $total['total_price_in'] += $value['price'];
                }else{
                    $total['total_price'] += $value['price'];
                }
            }
            $infos['total'] = $total;
            $infos['info'] = $info;
            return array_out(200, 'success', $infos);
        } else {
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param uid 	                int             会员ID
     * @param pay_price 		    decimal         支付金额
     * @param pay_order_id 		        int             订单号
     * @param song_price 		    decimal         赠送金额
     * @param pay_type 		        tinyint         支付类型 后台做个字典 1微信 2支付宝
     * @param create_time 			int             创建时间
     * @param update_time 			int             编辑时间
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function addData($uid, $pay_price, $pay_order_id, $pay_type){
        $data['uid']                        = $uid;
        $data['pay_price']                  = $pay_price;
        $data['pay_order_id']                   = $pay_order_id;
        $data['pay_type']                   = $pay_type;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 编辑数据
     * @param id 	                int             储值单ID
     * @param uid 	                int             会员ID
     * @param pay_price 		    decimal         支付金额
     * @param pay_order_id 		        int             订单号
     * @param pay_type 		        tinyint         支付类型 后台做个字典 1微信 2支付宝
     * @param create_time 			int             创建时间
     * @param update_time 			int             编辑时间
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
	public function editData($id, $uid, $pay_price, $pay_order_id,  $pay_type){
		$map = [];
		$map['id'] = $id;
		
		$data = [];
        $data['uid']                    = $uid;
        $data['pay_price']             = $pay_price;
        $data['pay_order_id']               = $pay_order_id;
        $data['pay_type']               = $pay_type;
        $res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
